﻿using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;

namespace LocationAwareness
{
    /// <summary>
    /// Class to display instances of type PoiView
    /// </summary>
    class PoiListPanel:Panel
    {
        private Panel panel1;
        private Panel panel2;
        private Panel refPanel;
        private Panel otherPanel;
        private Dictionary<string, PoiView> listPoi;

        public PoiListPanel()
        {
            listPoi = new Dictionary<string, PoiView>();
            //listPoi.Add("dummy", new PoiView(new POI()));

            panel1 = new Panel();
            panel2 = new Panel();

            this.SuspendLayout();

            panel1.Dock = DockStyle.Fill;
            panel1.AutoScroll = true;

            panel2.Dock = DockStyle.Fill;
            panel2.Visible = false;
            panel2.AutoScroll = true;

            this.Controls.Add(panel1);
            this.Controls.Add(panel2);

            this.ResumeLayout(false);
        }

        /// <summary>
        /// Sets backcolor of control.
        /// </summary>
        public override Color BackColor
        {
            set
            {
                base.BackColor = value;
                panel1.BackColor = value;
                panel2.BackColor = value;
            }

            get
            {
                return base.BackColor;
            }
        }

        /// <summary>
        /// Begins update of panel,
        /// hide panel PoiViews are added to.
        /// </summary>
        public void BeginUpdate()
        {
            if (panel1.Visible)
            {
                refPanel = panel2;
                otherPanel = panel1;
            }
            else
            {
                refPanel = panel1;
                otherPanel = panel2;
            }
        }

        /// <summary>
        /// Adds PoiView to hidden panel.
        /// </summary>
        /// <param name="pv">PoiView to be added</param>
        /// <returns>True if image of PoiView is cached in control, else false</returns>
        public bool AddPoiView(PoiView pv)
        {
            bool hasImage = false;

            //Einfügen nach dem ImagePath enthalten
            if (listPoi.ContainsKey(pv.ImagePath))
            {
                Bitmap bit = listPoi[pv.ImagePath].GetBitmap();

                if (bit != null)
                {
                    pv.SetBitmap(bit);
                    hasImage = true;
                }

                listPoi.Remove(pv.ImagePath);
            }

            listPoi.Add(pv.ImagePath, pv);
            refPanel.Controls.Add(pv);

            return hasImage;
        }

        /// <summary>
        /// Ends update, shows updated panel to user.
        /// </summary>
        public void EndUpdate()
        {
            otherPanel.Visible = false;
            otherPanel.Controls.Clear();
            refPanel.Visible = true;
        }

        /// <summary>
        /// Adds image to a PoiView shown in this instance
        /// </summary>
        /// <param name="path">Specifing the path of the image</param>
        /// <param name="bitmap">Instance of the image</param>
        public void AddImage(string path, Bitmap bitmap)
        {
            if (listPoi.ContainsKey(path))
            {
                listPoi[path].SetBitmap(bitmap);

                //PoiView poiView = (PoiView)refPanel[refPanel.Controls.GetChildIndex(listPoi[path])];
                //poiView.PoiImage = bitmap;
            }
        }
    }
}
